<?php

namespace App\Rpc\Traits\Service;


use App\Rpc\Model\Dao\RoleDao;
use Swoft\Bean\BeanFactory;
use App\Lib\Error\Response;
use App\Lib\Error\ErrorConstant;
use App\Rpc\Model\Logic\PermissionLogic;
use App\Rpc\Model\Dao\RolePermissionDao;

/**
 * Created by PhpStorm.
 * User: Admin
 * Date: 2020/5/9
 * Time: 14:47
 */
trait RoleTrait
{
    /**
     * @param array $data
     * @return array
     */
    public function allRoles(array $data): array
    {
        try{
            /** @var RoleDao $RoleDao */
            $RoleDao = BeanFactory::getBean(RoleDao::class);
            //默认过滤掉已经冻结的角色
            $data['where'] = ['frozen_state' => 0];
            $result = $RoleDao->getList($data);

            return Response::Info($result);
        }
        catch (\Exception $e)
        {
            return Response::FatalError($e->getCode(), $e->getMessage());
        }

        //参数：$data['p'] ...
        //参数：$data['per'] ...
        //列表展示
    }

    /**
     * @param array $data
     * @return array
     */
    public function roleCreate(array $data): array
    {
        //验证数据是否合理
        $userValidator = [
            'RoleValidator' => [ 'op' => 'add']
        ];
        try{
            /** @var RoleDao $RoleDao */
            $RoleDao = BeanFactory::getBean(RoleDao::class);

            //添加item_id
            $data['item_id'] = create_item_id(uuid());
            $data['frozen_state'] = 0;
            //这里首先验证中间件
            validate($data, "RoleValidator", [], $userValidator);
            $result = $RoleDao->insert($data);
            return Response::Info(['item_id' => $result]);
        }
        catch (\Exception $e){
            return Response::FatalError($e->getCode(), $e->getMessage());
        }
        //参数：$data['item_id'] ...
        //新增插入
    }

    /**
     * @param array $data
     * @return array
     */
    public function roleUpdate(array $data): array
    {
        //验证数据是否合理更新
        $userValidator = [
            'RoleValidator' => [ 'op' => 'update']
        ];
        try{
            //添加item_id
            $id = $data['item_id']??null;
            unset($data['item_id']);

            //这里首先验证中间件
            validate($data, "RoleValidator", [], $userValidator);
            $RoleDao = BeanFactory::getBean(RoleDao::class);
            $result = $RoleDao->update($id, $data);

            return Response::Info(['state' => $result]);
        }
        catch (\Exception $e){
            return Response::FatalError($e->getCode(), $e->getMessage());
        }

        //参数：$data['role_item_id']
        //参数：$data['update_data'] = []
        //修改某个角色
    }

    /**
     * @param array $data
     * @return array
     */
    public function roleDelete(array $data): array
    {
        try{
            //添加item_id
            $id = $data['item_id']??null;
            unset($data['item_id']);

            $RoleDao = BeanFactory::getBean(RoleDao::class);
            $result = $RoleDao->delete($id);

            return Response::Info(['state' => $result]);
        }
        catch (\Exception $e){
            return Response::FatalError($e->getCode(), $e->getMessage());
        }
        //参数：$data['role_item_id']
        //删除某个角色
    }

    /**
     * @param array $data
     * @return array
     */
    public function rolePermissions(array $data): array
    {
        try{
            //根据role_item_id获取所有permissions
            $role_id = $data['role_id']??null;
            if(!$role_id){
                //代表参数错误
                return Response::FatalError(ErrorConstant::REQUEST_PARAMS_ERROR_CODE, ErrorConstant::REQUEST_PARAMS_ERROR_TITLE);
            }

            //接下来查询所有得role_permissions
            /** @var PermissionLogic $PermissionLogic */
            $PermissionLogic = BeanFactory::getBean(PermissionLogic::class);
            $result = $PermissionLogic->rolePermissions($role_id);

            return Response::Info($result);
        }
        catch (\Exception $e){
            return Response::FatalError($e->getCode(), $e->getMessage());
        }
        //参数：$data['role_item_id']
        //获取某个角色下的所有权限
    }

    /**
     * @param array $data
     * @return array
     */
    public function roleModifyUsers(array $data): array
    {
        //根绝item_id添加用户人员
        try{
            $role_item_id = $data['role_item_id']??null;

            $attached_account_item_ids = $data['attached_account_item_ids']??[];

            $detached_account_item_ids = $data['detached_account_item_ids']??[];

            if(!$role_item_id){
                return Response::FatalError(ErrorConstant::REQUEST_PARAMS_ERROR_CODE, ErrorConstant::REQUEST_PARAMS_ERROR_TITLE);
            }

            if(!$attached_account_item_ids && !$detached_account_item_ids){
                return Response::FatalError(ErrorConstant::REQUEST_PARAMS_ERROR_CODE, ErrorConstant::REQUEST_PARAMS_ERROR_TITLE);
            }

            //接下来查询所有得role_permissions
            /** @var PermissionLogic $PermissionLogic */

            $PermissionLogic = BeanFactory::getBean(PermissionLogic::class);
            $result = $PermissionLogic->roleModifyUsers($data);
            return Response::Info($result);
        }
        catch (\Exception $e){
            return Response::FatalError($e->getCode(), $e->getMessage());
        }

        //参数：$data['role_item_id']
        //参数：$data['user_item_ids'] = []
        //更改某个角色的用户
    }

    /**
     * @param array $data
     * @return array
     */
    public function roleModifyPermissions(array $data): array
    {
        //根绝item_id添加or删除权限
        try{
            $role_item_id = $data['role_item_id']??null;

            $attached_permission_item_ids = $data['attached_permission_item_ids']??[];

            $detached_permission_item_ids = $data['detached_permission_item_ids']??[];

            if(!$role_item_id){
                return Response::FatalError(ErrorConstant::REQUEST_PARAMS_ERROR_CODE, ErrorConstant::REQUEST_PARAMS_ERROR_TITLE);
            }

            if(!$attached_permission_item_ids && !$detached_permission_item_ids){
                return Response::FatalError(ErrorConstant::REQUEST_PARAMS_ERROR_CODE, ErrorConstant::REQUEST_PARAMS_ERROR_TITLE);
            }

            //接下来查询所有得role_permissions
            /** @var PermissionLogic $PermissionLogic */

            $PermissionLogic = BeanFactory::getBean(PermissionLogic::class);
            $result = $PermissionLogic->roleModifyPermissions($data);
            return Response::Info($result);
        }
        catch (\Exception $e){
            return Response::FatalError($e->getCode(), $e->getMessage());
        }
    }

    /**
     * @param array $data
     * @return array
     */
    public function roleUsers(array $data): array
    {
        try{
            //参数验证
            $role_item_id = $data['role_item_id']??null;
            if(!$role_item_id){
                return Response::FatalError(4002, '请求参数错误');
            }

            //接下来查询所有得role_permissions
            /** @var PermissionLogic $PermissionLogic */
            $PermissionLogic = BeanFactory::getBean(PermissionLogic::class);
            $result = $PermissionLogic->roleUsers($role_item_id);
            return Response::Info($result);
        }
        catch (\Exception $e){
            return Response::FatalError($e->getCode(), $e->getMessage());
        }
        //这里首先要知道role_item_id
        //根据role_item_id取所有人员
        //参数：$data['role_item_id']
        //获取所有角色下的用户
    }

    /**
     * @param array $data
     * @return array
     */
    public function hasRole(array $data): array
    {
        try{
            //参数验证
            $role_item_id = $data['role_item_id']??null;
            $user_item_id = $data['user_item_id']??null;

            if(!$role_item_id || $user_item_id){
                return Response::FatalError(4002, '请求参数错误');
            }

            //接下来查询所有得role_permissions
            /** @var PermissionLogic $PermissionLogic */
            $PermissionLogic = BeanFactory::getBean(PermissionLogic::class);
            $result = $PermissionLogic->hasRole($data);
            return Response::Info($result);
        }
        catch (\Exception $e){
            return Response::FatalError($e->getCode(), $e->getMessage());
        }
        //首先：获取用户角色
        //      |-用户--角色
        //      |-组织--角色
        //      |-职位--角色
        //最后：判断是否拥有该角色
    }

    /**
     * finished
     * @param array $data
     * @return array
     */
    public function userRolePermissions(array $data): array
    {
        try{
            //参数验证
            $user_item_id = $data['user_item_id']??null;

            if(!$user_item_id){
                return Response::FatalError(4002, '请求参数错误');
            }

            //接下来查询所有得role_permissions
            /** @var PermissionLogic $PermissionLogic */
            $PermissionLogic = BeanFactory::getBean(PermissionLogic::class);
            $result = $PermissionLogic->userRolePermissions($data);
            return Response::Info($result);
        }
        catch (\Exception $e){
            return Response::FatalError($e->getCode(), $e->getMessage());
        }
        //参数：$data['user_item_id']
        //首先：获取用户角色
        //      |-用户--角色
        //      |-组织--角色
        //      |-职位--角色
        //其次：用户权限
        //最后：合集所有权限
    }

    /**
     * @param array $data
     * @return array
     */
    public function isUserHasPermission(array $data): array
    {
        try{
            //参数验证
            $permission_value = $data['permission_value']??null;
            $user_item_id = $data['user_item_id']??null;

            if(!$permission_value || !$user_item_id){
                return Response::FatalError(4002, '请求参数错误');
            }

            //接下来查询所有得role_permissions
            /** @var PermissionLogic $PermissionLogic */
            $PermissionLogic = BeanFactory::getBean(PermissionLogic::class);
            $result = $PermissionLogic->isUserHasPermission($data);
            return Response::Info($result);
        }
        catch (\Exception $e){
            return Response::FatalError($e->getCode(), $e->getMessage());
        }
        //参数：$data['user_item_id']
        //参数：$data['permission_value']
        //首先：获取用户角色
        //      |-用户--角色
        //      |-组织--角色
        //      |-职位--角色
        //其次：用户权限
        //最后：合集所有权限
        //判断是否该用户是否有权限
    }
}
